﻿using System;
using System.Web.Services;
using Monkey.Warrenty.Biz;
using Monkey.Warrenty.Enum;
using MonkeyWarranty.Web.Common;

namespace MonkeyWarranty.Web
{
    public partial class _Default : System.Web.UI.Page
    {
        #region Properties

        private static InstantTicketBiz _instantTicketBiz;
        private static InstantTicketBiz InstantTicketManager
        {
            get { return _instantTicketBiz ?? (_instantTicketBiz = new InstantTicketBiz()); }
        }

        protected int NewTicketCount
        {
            get { return InstantTicketManager.GetTicketCount(InstantTicketStatus.Received); }
        }

        protected int ProcessingTicketCount
        {
            get { return InstantTicketManager.GetTicketCount(InstantTicketStatus.Processing); }
        }

        //protected int CompletedTicketCount
        //{
        //    get { return InstantTicketManager.GetTicketCount(InstantTicketStatus.Completed); }
        //}

        private static WarrantyTicketBiz _warrantyTicketBiz;
        private static WarrantyTicketBiz WarrantyTicketManager
        {
            get { return _warrantyTicketBiz ?? (_warrantyTicketBiz = new WarrantyTicketBiz()); }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            //if(!IsPostBack)
            //{
            //    rptTicketEmployeeCounts.DataSource = InstantTicketManager.GetInstantTicketEmployeeCount(
            //           new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1), DateTime.Today);
            //    rptTicketEmployeeCounts.DataBind();
            //}
        }

        [WebMethod(EnableSession = true)]
        public static object TicketListByFilter(int jtStartIndex, int jtPageSize, string jtSorting)
        {
            //System.Threading.Thread.Sleep(1000);
            var ticketCount = WarrantyTicketManager.GetActiveTicketCount(jtStartIndex, jtPageSize, jtSorting);
            var warrantyTicketViews = WarrantyTicketManager.GetActiveTickets(jtStartIndex, jtPageSize, jtSorting);

            return new { Result = "OK", Records = warrantyTicketViews, TotalRecordCount = ticketCount };
        }

        [WebMethod(EnableSession = true)]
        public static object Statistic()
        {
            var newCount = StatisticBiz.CountWarrantyTickets(1);
            var processingCount = StatisticBiz.CountWarrantyTickets(2);
            var processingProductsCount = StatisticBiz.CountProcessingProducts();
            var receivedProductsCount = StatisticBiz.CountReceivedProducts();
            return new
            {
                NewCount = newCount,
                ProcessingCount = processingCount,
                ProcessingProductsCount = processingProductsCount,
                ReceivedProductsCount = receivedProductsCount
            };
        }

        [WebMethod(EnableSession = true)]
        public static object TicketServiceList()
        {
            var ticketServices = InstantTicketManager.GetActiveTicketServiceViews(WarrantyCommon.CurrrentEmployee);
            return new { Result = "OK", Records = ticketServices };
        }

        [WebMethod(EnableSession = true)]
        public static object TicketProductList()
        {
            var ticketProducts = InstantTicketManager.GetActiveTicketProductViews(WarrantyCommon.CurrrentEmployee);
            return new { Result = "OK", Records = ticketProducts };
        }
    }
}
